import configparser
import json
from pathlib import Path

import pytest

from utils.client import APIClient


@pytest.fixture(scope='session')
def api_client():
    # return APIClient(besa_url= 'https://erp-api.wecharmer.com/')
    # test环境
    return APIClient(besa_url= 'http://192.168.5.197:8090/')


@pytest.fixture
def me_online_token(api_client):
    param = {'tenantName': 'Wecharmer.Hero', 'account': 'xuluqiang', 'password': 'xlqQa123456*'}
    response = api_client.post('api/account/login'
                               ,data = json.dumps(param)
                               )
    authorization = response.json()['result']
    return {'Authorization': authorization}


@pytest.fixture
def me_test_token(api_client, config_road):
    param = {'tenantName': 'Wecharmer.Hero', 'account': 'xuluqiang', 'password': 'Aa123456!'}
    response = api_client.post('api/account/login'
                               ,data = json.dumps(param)
                               )
    authorization = response.json()['result']
    return {'authorization': authorization}

@pytest.fixture
def admin_token(api_client):
    response = api_client.post('proxy/account/login'
                               ,data = {"account":"admin","password":"123qweR!","tenantName":"Wecharmer.Hero"})
    authorization = response.json().get('result')
    return {'Authorization': authorization}

@pytest.fixture
def config_road():
    config_path = Path(__file__).parent / 'pytest.ini'
    print(config_path)
    if not config_path.exists():
        raise FileNotFoundError("配置文件 config.ini 未找到！")

    config = configparser.ConfigParser()
    config.read(config_path, encoding='utf-8')

    # config_path = r'D:\PythonProject\api_test_erp\config.ini'
    # with open(config_path, 'r', encoding='utf-8') as f:
    #     config_value = f.read()
    #     print(config_value)
    return config